package com.heima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heima.reggie.bean.Employee;
import com.heima.reggie.bean.PageParam;
import com.heima.reggie.common.R;
import com.heima.reggie.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


@Api(tags = "员工管理")
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /**
     * 员工登录！
     * @param employee
     * @param session
     * @return
     */
  /* @PostMapping("/login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee) {
        Employee emp = employeeService.login(employee);
       System.out.println("aaaaaaaaaaa");
        if (emp == null) {
            return R.error("登陆失败");
        }
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        session.setAttribute("employee", emp);

        return R.success(emp);
    }*/
    /**
     * 员工退出登录！
     * @param session
     * @return
     */
    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public R loginout(HttpSession session) {
        try {
            //清除session
            session.invalidate();
            return R.success("退出成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("退出失败！");
        }
    }
    /**
     * 添加员工！
     * @param employee
     * @param session
     * @return
     */
    @ApiOperation(value = "添加员工")
    @PostMapping
    public R add(@RequestBody Employee employee, HttpSession session) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setStatus(1);
       /* employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Employee employee1 = (Employee) session.getAttribute("employee");
        employee.setCreateUser(employee1.getId());
        employee.setUpdateUser(employee1.getId());*/
        int add = employeeService.add(employee);
        if (add > 0) {
            return R.success("添加成功！");
        } else {

            return R.error("添加失败！");
        }
    }
    /**
     * 员工列表分页查询！
     * @param pageParam
     * @return
     */
    @ApiOperation(value = "按条件分页查询")
    @GetMapping("/page")
    public R selectByPageAndCondition(PageParam pageParam) {
        IPage<Employee> employeeIPage = employeeService.selectByPageAndCondition(pageParam);
        if (employeeIPage != null) {
            /*Long currentId = BaseContext.getCurrentId();
            System.out.println(currentId);*/
            return R.success(employeeIPage);
        } else {
            return R.error("查询失败！");
        }
    }
    /**
     * 更新员工！
     * @param employee
     * @param session
     * @return
     */
    @ApiOperation(value = "更新员工")
    @PutMapping
    public R update(@RequestBody Employee employee, HttpSession session) {
        Employee employee1 = (Employee) session.getAttribute("employee");
        System.out.println(employee);
        Long id = employee1.getId();
        employee.setUpdateUser(id);
        employee.setUpdateTime(LocalDateTime.now());
        int update = employeeService.update(employee);
        if (update > 0) {
            return R.success("更新成功！");
        } else {
            return R.error("更新失败！");
        }
    }

    /*@GetMapping("/{id}")
    public R selectById(@PathVariable Long id) {
        //Employee employee = (Employee) session.getAttribute("employee");
        System.out.println(id);
        Employee employee = employeeService.selectById(id);
        if (employee != null) {
            return R.success(employee);
        } else {
            return R.error("查询失败！");
        }
    }*/
}
